package com.chy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.common.BaseContext;
import com.chy.common.R;
import com.chy.common.SnowflakeIdUtils;
import com.chy.entity.Router;
import com.chy.mapper.RouterMapper;
import com.chy.service.RouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> implements RouterService {

    @Autowired
    RouterMapper routerMapper;

    @Override
    public R<Router> getRouter(Map<String, Object> params) {
        List<Router> list = routerMapper.getRouterList(params);
        return R.success(getChildPerms(list, 0L));
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param  list     分类表
     * @param  parentId 传入的父节点ID
     * @return          String
     */
    public Router getChildPerms(List<Router> list, Long parentId) {
//        List<Router> returnList = new ArrayList<Router>();
        Router router = new Router();
        for (Iterator<Router> iterator = list.iterator(); iterator.hasNext();) {
            Router t = iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getRouterId().equals(parentId)) {
                recursionFn(list, t);
                router = t;
            }
        }
        return router;
    }
    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<Router> list, Router t) {
        // 得到子节点列表
        List<Router> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Router tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<Router> getChildList(List<Router> list, Router t) {
        List<Router> tlist = new ArrayList<Router>();
        Iterator<Router> it = list.iterator();
        while (it.hasNext()) {
            Router n = (Router) it.next();
            if (n.getParentId().equals(t.getRouterId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Router> list, Router t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }





    @Override
    public R<String> addRouter(HttpServletRequest request, Map<String, Object> params) {
        Router router = JSON.parseObject(JSON.toJSONString(params), Router.class);
        if (params.get("id").equals("")) {
            long snowId = new SnowflakeIdUtils(1, 1).nextId();
            router.setRouterId(snowId);
        }
        router.setCreateTime(LocalDateTime.now());
        router.setUpdateTime(LocalDateTime.now());
        router.setCreateUser(0L);
        router.setUpdateUser(0L);
        return routerMapper.insertRouter(router) ? R.success() : R.error();
    }

    @Override
    public R<String> updateRouter(HttpServletRequest request, Map<String, Object> params) {
        Router router = JSON.parseObject(JSON.toJSONString(params), Router.class);

        return routerMapper.updateRouter(router) ? R.success() : R.error();
    }

    @Override
    public R<String> deleteRouter(HttpServletRequest request, Map<String, Object> params) {
        Router router = JSON.parseObject(JSON.toJSONString(params), Router.class);
        return routerMapper.removeRouter(router) ? R.success() : R.error();
    }
}
